package com.example.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.orderassembly.controller.ProductOrderDetailController;
import com.woniuxy.form.*;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.AfterSale;
import com.woniuxy.entity.GoodOrder;
import com.example.orderassembly.mapper.GoodOrderMapper;
import com.example.orderassembly.service.IGoodOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 商品订单表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
@SuppressWarnings("all")
public class GoodOrderServiceImpl extends ServiceImpl<GoodOrderMapper, GoodOrder> implements IGoodOrderService {
    @Resource
    GoodOrderMapper goodOrderMapper;
    @Resource
    ProductOrderDetailController productOrderDetailController;

    @Override
    public IPage getGoodOrderByUserId(Integer userId, Integer indexPage, Integer pageSize) {
        Page<GoodOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(indexPage);
        goodOrderPage.setSize(pageSize);
        QueryWrapper<GoodOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        goodOrderQueryWrapper.eq("good_order_user_id",userId);
        IPage<GoodOrder> goodOrderIPage = goodOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        return goodOrderIPage;
    }

    @Override
    public IPage getGoodOrderByStore(Integer storeId, GetAfterSaleByPlatform getAfterSaleByPlatform) {
        Page<GoodOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(getAfterSaleByPlatform.getIndexPage());
        goodOrderPage.setSize(getAfterSaleByPlatform.getPageSize());
        QueryWrapper<GoodOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        goodOrderQueryWrapper.eq("good_order_store_id",storeId);
        goodOrderQueryWrapper.eq(getAfterSaleByPlatform.getStatus()!=null,"good_order_status",getAfterSaleByPlatform.getStatus());
        goodOrderQueryWrapper.between(getAfterSaleByPlatform.getStartDate()!=null&&getAfterSaleByPlatform.getEndDate()!=null,"row_add_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
        IPage<GoodOrder> goodOrderIPage = goodOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        return goodOrderIPage;
    }

    @Override
    public IPage getGoodOrderByPlatform(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        Page<GoodOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(getAfterSaleByPlatform.getIndexPage());
        goodOrderPage.setSize(getAfterSaleByPlatform.getPageSize());
        QueryWrapper<GoodOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        goodOrderQueryWrapper.eq(getAfterSaleByPlatform.getStatus()!=null,"good_order_status",getAfterSaleByPlatform.getStatus());
        goodOrderQueryWrapper.between(getAfterSaleByPlatform.getStartDate()!=null&&getAfterSaleByPlatform.getEndDate()!=null,"row_add_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
        IPage<GoodOrder> goodOrderIPage = goodOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        return goodOrderIPage;
    }

    @Override
    public IPage getGoodOrder(GetGoodOrderForm form) {
        Page<GoodOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(form.getIndexPage());
        goodOrderPage.setSize(form.getPageSize());
        QueryWrapper<GoodOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        goodOrderQueryWrapper.eq(StringUtils.isNotBlank(form.getGoodOrderDeliveryMethod()) ,"good_order_delivery_method",form.getGoodOrderDeliveryMethod());
        goodOrderQueryWrapper.between(form.getGoodOrderStartTime()!=null&&form.getGoodOrderStatus()!=null,"good_order_create_time",form.getGoodOrderStartTime(),form.getGoodOrderEndTime());
        goodOrderQueryWrapper.eq(form.getGoodOrderStatus()!=null,"good_order_status",form.getGoodOrderStatus());
        if(form.getGoodOrderTotal()!=null){
            if (form.getGoodOrderTotalType()) {
                goodOrderQueryWrapper.ge("good_order_total", form.getGoodOrderTotal());
            } else {
                goodOrderQueryWrapper.le("good_order_total", form.getGoodOrderTotal());
            }
        }
        IPage<GoodOrder> goodOrderIPage = goodOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        return goodOrderIPage;
    }

    @Override
    public Boolean updateGoodOrderStatusById(Integer goodOrderId, Integer status) {
        GoodOrder goodOrder = goodOrderMapper.selectById(goodOrderId);
        System.err.println(goodOrder);
        if(status<=goodOrder.getGoodOrderStatus()){
            return false;
        }
        // 0:未付款 1:已付款  2:待自提  3: 已自提 4:已发货 5:物流中  6:已签收  7:申请退货
        // 8:申请换货  9:退货完成  10:订单完成   11:换货完成，12：未评论  13：已评论 14取消订单
        if((goodOrder.getGoodOrderStatus()==0&&(status==1||status==14))||
                (goodOrder.getGoodOrderStatus()==1&&(status==2||status==4))||
                (goodOrder.getGoodOrderStatus()==2&&status==3)||
                (goodOrder.getGoodOrderStatus()==3&&status==7)||
                (goodOrder.getGoodOrderStatus()==3&&status==8)||
                (goodOrder.getGoodOrderStatus()==3&&status==10)||
                (goodOrder.getGoodOrderStatus()==4&&status==5)||
                (goodOrder.getGoodOrderStatus()==4&&status==7)||
                (goodOrder.getGoodOrderStatus()==4&&status==8)||
                (goodOrder.getGoodOrderStatus()==5&&status==7)||
                (goodOrder.getGoodOrderStatus()==5&&status==6)||
                (goodOrder.getGoodOrderStatus()==5&&status==8)||
                (goodOrder.getGoodOrderStatus()==6&&status==7)||
                (goodOrder.getGoodOrderStatus()==6&&status==8)||
                (goodOrder.getGoodOrderStatus()==6&&status==10)||
                (goodOrder.getGoodOrderStatus()==7&&status==9)||
                (goodOrder.getGoodOrderStatus()==8&&status==11)||
                (goodOrder.getGoodOrderStatus()==10&&status==12)||
                (goodOrder.getGoodOrderStatus()==11&&status==12)||
                (goodOrder.getGoodOrderStatus()==12&&status==13)){
            System.err.println("进来了");
            UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
            goodOrderUpdateWrapper.eq("good_order_id",goodOrderId);
            goodOrderUpdateWrapper.set("good_order_status",status);
            int update = goodOrderMapper.update(null, goodOrderUpdateWrapper);
            return update==1;
        }else {
            System.err.println("没进入");
        }
        return false;

    }

    @Override
    public Integer insertGoodOrder(InsertGoodOrder insertGoodOrder) {
        GoodOrder goodOrder = new GoodOrder();
        BeanUtils.copyProperties(insertGoodOrder,goodOrder);
        //todo 取到JWT 用户id
        Integer userId = 1;
        goodOrder.setGoodOrderUserId(userId);
        goodOrder.setGoodOrderStatus(0);
        goodOrder.setGoodOrderCreateTime(new Date());
        System.out.println(goodOrder);
        int insert = goodOrderMapper.insert(goodOrder);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < insertGoodOrder.getGoodIdList().size(); i++) {
            map.put(insertGoodOrder.getGoodIdList().get(i),insertGoodOrder.getGoodNumList().get(i));
        }
        InsertGoodOrderDetail insertGoodOrderDetail = new InsertGoodOrderDetail();
        insertGoodOrderDetail.setGoodAttribute(map);
        insertGoodOrderDetail.setGoodOrderId(goodOrder.getGoodOrderId());
        productOrderDetailController.insertProductOrderDetail(insertGoodOrderDetail);
        return goodOrder.getGoodOrderId();
    }

    @Override
    public Boolean updateGoodOrderStatus(Integer goodOrderId) {
        GoodOrder goodOrder = goodOrderMapper.selectById(goodOrderId);
        if(goodOrder.getGoodOrderStatus()!=0){
            return false;
        }
        UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
        goodOrderUpdateWrapper.eq("good_order_id",goodOrderId);
        goodOrderUpdateWrapper.set("good_order_status",-1);
        int update = goodOrderMapper.update(null, goodOrderUpdateWrapper);
        return update == 1;
    }

    @Override
    public Boolean updateGoodOrderPaied(Integer goodOrderId) {
        GoodOrder goodOrder = goodOrderMapper.selectById(goodOrderId);
        if(goodOrder.getGoodOrderStatus()!=0){
            return false;
        }
        UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
        goodOrderUpdateWrapper.eq("good_order_id",goodOrderId);
        goodOrderUpdateWrapper.set("good_order_status",1);
        int update = goodOrderMapper.update(null, goodOrderUpdateWrapper);
        return update == 1;
    }

    @Override
    public Boolean updateGoodOrderReceiving(Integer goodOrderId) {
        GoodOrder goodOrder = goodOrderMapper.selectById(goodOrderId);
        if(goodOrder.getGoodOrderStatus()!=4&&goodOrder.getGoodOrderStatus()!=3){
            return false;
        }
        UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
        goodOrderUpdateWrapper.eq("good_order_id",goodOrderId);
        goodOrderUpdateWrapper.set("good_order_status",6);
        int update = goodOrderMapper.update(null, goodOrderUpdateWrapper);
        return update == 1;
    }

    @Override
    public Boolean updateGoodOrderEvaluated(Integer goodOrderId) {
        GoodOrder goodOrder = goodOrderMapper.selectById(goodOrderId);
        if(goodOrder.getGoodOrderStatus()!=12){
            return false;
        }
        UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
        goodOrderUpdateWrapper.eq("good_order_id",goodOrderId);
        goodOrderUpdateWrapper.set("good_order_status",13);
        int update = goodOrderMapper.update(null, goodOrderUpdateWrapper);
        return update == 1;
    }

    @Override
    public GoodOrder getGoodOrderById(Integer id) {
        GoodOrder goodOrder = goodOrderMapper.selectById(id);
        return goodOrder;
    }

    @Override
    public Boolean updateGoodOrderPayType(Integer orderId, Integer payType) {
        UpdateWrapper<GoodOrder> goodOrderUpdateWrapper = new UpdateWrapper<>();
        goodOrderUpdateWrapper.eq("good_order_id",orderId).set("good_order_pay_type",payType);
        int update = getBaseMapper().update(null, goodOrderUpdateWrapper);
        return update==1;

    }
}
